package com.hl.controller.orders;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hl.domain.OrderMain;
import com.hl.service.IOrderMainService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品订单Controller
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@RestController
@RequestMapping("/order")
public class OrderMainController extends BaseController
{
    @Autowired
    private IOrderMainService orderMainService;

    /**
     * 查询商品订单列表
     */
//    @RequiresPermissions("orders:order:list")
    @GetMapping("/list")
    public TableDataInfo list(OrderMain orderMain)
    {
        System.out.println(orderMain+"接收");
        startPage();
        List<OrderMain> list = orderMainService.selectOrderMainList(orderMain);
        return getDataTable(list);
    }

    /**
     * 导出商品订单列表
     */
    @RequiresPermissions("orders:order:export")
    @Log(title = "商品订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderMain orderMain)
    {
        List<OrderMain> list = orderMainService.selectOrderMainList(orderMain);
        ExcelUtil<OrderMain> util = new ExcelUtil<OrderMain>(OrderMain.class);
        util.exportExcel(response, list, "商品订单数据");
    }

    /**
     * 通过店铺ID查询订单信息
     */
    @GetMapping(value = "/shop/{shopId}")
    public AjaxResult getOrdersByShopId(@PathVariable("shopId") Integer shopId)
    {
        List<OrderMain> list = orderMainService.selectOrderMainByShopId(shopId);
        return success(list);
    }

    /**
     * 获取商品订单详细信息
     */
    @RequiresPermissions("orders:order:query")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return success(orderMainService.selectOrderMainByOrderId(orderId));
    }

    //获取订单商品表信息
//    @RequiresPermissions("orders:order:query")
//    @GetMapping( "/getOrderProduct/{orderId}")
//    public AjaxResult getOrderProduct(@PathVariable("orderId") String orderId)
//    {
//        return success(orderMainService.selectOrderProductByOrderId(orderId));
//    }

    /**
     * 新增商品订单
     */
    @RequiresPermissions("orders:order:add")
    @Log(title = "商品订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderMain orderMain)
    {
        System.out.println(orderMain+"新增");
        return toAjax(orderMainService.insertOrderMain(orderMain));
    }

    /**
     * 修改商品订单
     */
    @RequiresPermissions("orders:order:edit")
    @Log(title = "商品订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderMain orderMain)
    {
        return toAjax(orderMainService.updateOrderMain(orderMain));
    }

    /**
     * 删除商品订单
     */
    @RequiresPermissions("orders:order:remove")
    @Log(title = "商品订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds)
    {
        return toAjax(orderMainService.deleteOrderMainByOrderIds(orderIds));
    }

    /**
     * 修改订单支付状态
     */
    @RequiresPermissions("orders:order:edit")
    @Log(title = "修改支付状态", businessType = BusinessType.UPDATE)
    @PutMapping("/edit/{orderId}/{orderStatus}/{paymentStatus}")
    public AjaxResult updatePaymentStatus(@PathVariable("orderId") @Param("orderId") String orderId,
                                        @PathVariable("orderStatus") @Param("orderStatus") String orderStatus,
                                          @PathVariable("paymentStatus") @Param("paymentStatus") String paymentStatus)
    {
        return toAjax(orderMainService.updateOrderPaymentStatus(orderId, orderStatus,paymentStatus));
    }
}
